package org.jeecg.modules.cascv.image.tools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.HexUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.cascv.image.service.merge.WurongqiService;

import java.io.*;
import java.util.List;

/**
 * @author;作者
 * @Date 2023/5/17 16:30
 * @Description :
 * @Version
 */
public class ShiPinToH264 {
    public static void main(String[] args) throws IOException {
        //
        //E:\11图像数据\cut\变重力
      //  txt("E:\\11图像数据\\cut2\\流体\\T20221030131630_0001_006B_F1_00_6DCD_2024_002F_2F_6D26_sp.txt");
        txt("E:\\11111图像数据整理\\舱2\\流体柜\\h264\\流体动力学全景相机图像数据_2F_6D26.h264");
       // bes("E:\\11图像数据\\cut\\变重力\\T20210429113130_0001_0096_F1_00_ED1D_2024_0020_20_6D3D_000000000.dat");
       // bes("E:\\11111图像数据整理\\shuju\\流体柜\\T20230407110803_0001_006B_F1_00_6DCD_2024_002F_2F_6D28_000000000.dat");
    }

    public static void txt(String file) {
        List<String> strings = FileUtil.readUtf8Lines(file);
        File tofile = new File("E:\\11111图像数据整理\\舱2\\流体柜\\h264\\2F_6D26_sp.h264");
        FileWriter fileWriter = new FileWriter(tofile);
        for (String string : strings) {
            JSONObject jsonObject = JSONObject.parseObject(string);
            jsonObject= jsonObject.getJSONObject("Content");
            String videoStr = jsonObject.getString("A1_FPR_MICU_APP7DATA_17_2021_1");
//            videoStr = StringUtils.substring(videoStr, 7, videoStr.length() - 16);
            videoStr = (videoStr.split(";")[1]).substring(2).trim();
            byte[] bytes = HexUtil.decodeHex(videoStr);
            fileWriter.append(bytes,0,bytes.length);
        }
    }

    public static void bes(String file) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        byte[] bytes = new byte[2024];
        File tofile = new File("E:\\11111图像数据整理\\shuju\\流体柜\\6d28.h264");
        FileWriter fileWriter = new FileWriter(tofile);
        while(true){
            int read = randomAccessFile.read(bytes);
            if(read <= 0){
                break;
            }
            String str = HexUtil.encodeHexStr(bytes);
            String frameStr = str.substring(32, 4044);
            byte[] bytes1 = HexUtil.decodeHex(frameStr);
            fileWriter.append(bytes1,0,bytes1.length);
        }
    }

}
